package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
public class DishServiceIml implements DishService {

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private DishFlavorMapper dishFlavorMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Override
    @Transactional

    public void save(DishDTO dishDTO) {

        Dish dish1 = new Dish();
        BeanUtils.copyProperties(dishDTO, dish1);
        dish1.setCreateTime(LocalDateTime.now());
        dish1.setUpdateTime(LocalDateTime.now());

        dishMapper.insert(dish1);

        long dishId = dish1.getId();

        List<DishFlavor> flavors = dishDTO.getFlavors();

        if (flavors != null && flavors.size() > 0) {
            flavors.forEach(flavor -> {
                flavor.setDishId(dishId);
            });

            dishFlavorMapper.insertBatch(flavors);
        }
    }

    @Override
    public PageResult pageQuery(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());
        Page<DishVO> page = dishMapper.pageQuery(dishPageQueryDTO);
        long total = page.getTotal();
        List<DishVO> records = page.getResult();
        return new PageResult(total, records);
    }

    @Override
    @Transactional
    public void delete(List<Long> ids) {
        //判断是否有起售中的菜品
        ids.forEach(id -> {
            Dish dish = dishMapper.selectById(id);
            if (dish.getStatus() == 1) {
                throw new IllegalArgumentException("有起售中的菜品，不能删除");
            }

        });


        //判断当前菜品是否能够删除，是否有套餐关联
        List<Long> count = setmealDishMapper.deleteBySetmealId(ids);
        if (count != null && count.size() > 0) {
            throw new IllegalArgumentException("当前菜品有套餐关联，不能删除");
        }


            //删除菜品关联的口味
//            dishFlavorMapper.deleteByDishId(Collections.singletonList(id));
            //删除菜品
            dishMapper.deleteById(ids);
            //删除套餐关联的菜品
//            setmealDishMapper.deleteBySetmealId(ids);

    }

    @Override
    public DishDTO getById(Long id) {
        Dish dish = dishMapper.selectById(id);
        DishDTO dishDTO = new DishDTO();
        BeanUtils.copyProperties(dish, dishDTO);
        List<DishFlavor> flavors = dishFlavorMapper.selectByDishId(id);
        dishDTO.setFlavors(flavors);
        return dishDTO;
    }

    @Override
    @Transactional
    public void update(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        dish.setUpdateTime(LocalDateTime.now());
        dish.setUpdateUser(dish.getUpdateUser());
        dishMapper.update(dish);

        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors != null && flavors.size() > 0) {
            //删除原有的口味
            dishFlavorMapper.deleteByDishId(Collections.singletonList(dishDTO.getId()));
            //插入新的口味
            flavors.forEach(flavor -> {
                flavor.setDishId(dishDTO.getId());
            });
            dishFlavorMapper.insertBatch(flavors);
        }
    }

    /**
     * 条件查询菜品和口味
     * @param dish
     * @return
     */
    public List<DishVO> listWithFlavor(Dish dish) {
        List<Dish> dishList = dishMapper.list(dish);

        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFlavorMapper.selectByDishId(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;
    }

    @Override
    public void updateStatus(Integer status,Long id) {
        Dish dish = new Dish();
        dish.setId(id);

            dish.setStatus(status);
            dish.setUpdateTime(LocalDateTime.now());
            dish.setUpdateUser(dish.getUpdateUser());
            dishMapper.update(dish);
        };
    }

